function [CPX_OUT,TEMP] = fractionateAugite_V3_Yang1996(DAT,P, KDCPX)




 targetElements = {'SiO2' 'TiO2' 'Al2O3' 'Fe2O3' 'FeO' 'MgO' 'CaO' 'Na2O' 'K2O' 'P2O5' 'Cr2O3' 'MnO'}; 
% 
% [A,ElementIndicies4Target] = ismember(targetElements, inputElements)
% mat2clip(ElementIndicies4Target)

ElementIndicies4Target=[1.00000	2.00000	3.00000	0.00000	5.00000	7.00000	8.00000	9.00000	10.00000	11.00000	4.00000	6.00000];

noData = find(ElementIndicies4Target==0); 
ElementIndicies4Target(ElementIndicies4Target == 0) = max(ElementIndicies4Target); 
newlyOrderedElements = DAT(:,ElementIndicies4Target);
%pads rows of NaNs for elements with missing data
newlyOrderedElements(:,noData)=[0]; 
newlyOrderedElements(isnan(newlyOrderedElements))=0; 
DAT = newlyOrderedElements;  



% 1-SIO2 2-TIO2 3-AL2O3 4-FE2O3 5-FEO 6-MGO 7-CAO 8-NA2O 9-K2O 10-P2O5
% 11-CR2O3 12-MNO;


%%


TEMP=0.4389*DAT(1,7)+4.14*DAT(1,8)-3.103*DAT(1,2);
TEMP=TEMP+976;
TEMP=TEMP+4.756*P;
TEMP=TEMP+21.715*DAT(1,6);
TEMP=TEMP+2.509*DAT(1,5);
MSI=DAT(1,1)/60.09;
MTI=DAT(1,2)/79.9;
MAL=DAT(1,3)/102*2;
MFE2O3=DAT(1,4)/159.7*2;
MFE=DAT(1,5)/71.85;
MMG=DAT(1,6)/40.3;
MCA=DAT(1,7)/56.08;
MNA=DAT(1,8)/62*2;
MK=DAT(1,9)/94.2*2;
MP=DAT(1,10)/141.9*2;
MCR=DAT(1,11)/152*2;
MMN=DAT(1,12)/70.94;
MTOTAL=MSI+MTI+MAL+MFE2O3+MFE+MMG+MCA+MNA+MK+MP+MCR+MMN;
MSI=MSI/MTOTAL*100;
MTI=MTI/MTOTAL*100;
MAL=MAL/MTOTAL*100;
MFE2O3=MFE2O3/MTOTAL*100;
MFE=MFE/MTOTAL*100;
MMG=MMG/MTOTAL*100;
MCA=MCA/MTOTAL*100;
MNA=MNA/MTOTAL*100;
MK=MK/MTOTAL*100;
MP=MP/MTOTAL*100;
MCR=MCR/MTOTAL*100;
MMN=MMN/MTOTAL*100;
MCPXCA=25.044+1.6237*MCA-0.83215*MFE-0.9531*MMG-0.6167*MAL+0.4457*(MNA+MK);
% C	MCPXTI=-36.704+0.4661*MCA+0.3654*MFE+0.2590*MMG
% C     +         +0.566*MAL+0.3825*(MNA+MK)+0.7905*MTI
% C     +         +0.3020*MSI-0.034364*P
MCPXTI=-3.6124+0.022523*MCA+0.0339*MFE+0.10776*MAL+0.0426*(MNA+MK)...
    +0.3203*MTI-0.02169*P+0.001046*TEMP;
% C        MCPXAL=-31.4919+0.5045*MCA+0.3309*MFE+1.6012*MAL
% C     +         +1.2832*MTI
MCPXNA=-2.2955+0.082*MNA+0.002214*TEMP-0.02024*MMG;
MCPXMGFE=-11.051-1.0582*MCA+1.0893*MFE+1.1713*MMG+0.3848*MAL+0.5044*MSI;
MCPXSI=116.082-0.31039*MSI-1.374*MTI-1.5822*MAL-0.5334*MFE-0.706*...
    MCA-0.4177*MNA+0.2044*P-0.011125*TEMP;
% C	MCPXALT=2*MCPXTI
% C	MCPXALO=50-MCPXMGFE-MCPXCA-MCPXTI-MCPXNA
% C        MCPXALT=50-MCPXSI
% C	MCPXAL=MCPXALO+MCPXALT
MCPXMG=((KDCPX*MFE/MMG)+1)/MCPXMGFE;
MCPXMG=1/MCPXMG;
MCPXFE=MCPXMGFE-MCPXMG;
MCPXMG=MCPXMGFE*MCPXMG/(MCPXMG+MCPXFE);
MCPXFE=MCPXMGFE*MCPXFE/(MCPXMG+MCPXFE);
MCPXAL=98.2359-0.96422*MCPXSI-0.7958*MCPXTI-0.997656*MCPXMG-...
    1.02176*MCPXFE-1.01482*MCPXCA-0.931973*MCPXNA;
% C	MTOTAL=MCPXCA+MCPXTI+MCPXNA+MCPXMGFE+MCPXALO
% C	WRITE (9,1001) MTOTAL,MCPXTI,MCPXAL
% C1001    FORMAT (3F7.2)
% C	MCPXSI=50-MCPXALT-MCPXTI
CPXSIO2=MCPXSI*60.09;
CPXTIO2=MCPXTI*79.9;
CPXAL2O3=MCPXAL*102/2;
CPXFEO=MCPXFE*71.85;
CPXMGO=MCPXMG*40.3;
CPXCAO=MCPXCA*56.08;
CPXNA2O=MCPXNA*62/2;
TOTAL=CPXSIO2+CPXTIO2+CPXAL2O3+CPXFEO+CPXMGO+CPXCAO+CPXNA2O;
CPXSIO2=CPXSIO2/TOTAL*100;
CPXTIO2=CPXTIO2/TOTAL*100;
CPXAL2O3=CPXAL2O3/TOTAL*100;
CPXFEO=CPXFEO/TOTAL*100;
CPXMGO=CPXMGO/TOTAL*100;
CPXCAO=CPXCAO/TOTAL*100;
CPXNA2O=CPXNA2O/TOTAL*100;

CPXCOMP  =[ CPXSIO2 CPXTIO2 CPXAL2O3 CPXFEO CPXMGO CPXCAO CPXNA2O];

ElementsYANG = {'SiO2' 'TiO2' 'Al2O3' 'FeO' 'MgO' 'CaO' 'Na2O'}; 

% [A,ElementIndicies4Target] = ismember(targetElements, ElementsYANG);
% mat2clip(ElementIndicies4Target)

ElementIndicies4Target=[1.00000	2.00000	3.00000	0.00000	4.00000	5.00000	6.00000	7.00000	0.00000	0.00000	0.00000	0.00000];
noData = find(ElementIndicies4Target==0); 
ElementIndicies4Target(ElementIndicies4Target == 0) = max(ElementIndicies4Target); 
newlyOrderedElements = CPXCOMP(:,ElementIndicies4Target);
%pads rows of NaNs for elements with missing data
newlyOrderedElements(:,noData)=[0]; 
newlyOrderedElements(isnan(newlyOrderedElements))=0; 
%CPX_OUT = newlyOrderedElements; 
CPX_OUT = newlyOrderedElements;
% 
% CPXSIO2 =DAT(1,:); 
% CPXTIO2=DAT(2,:); 
% CPXAL2O3=DAT(3,:); 
% CPXFEO=DAT(4,:); 
% CPXMGO=DAT(5,:); 
% CPXCAO=DAT(6,:); 
% CPXNA2O=DAT(7,:); 

%          [CPXSIO2,CPXTIO2,CPXAL2O3,CPXFEO,CPXMGO,CPXCAO,CPXNA2O,TEMP] = ...
%     OPAMCPX_TRACE(DAT,P,KDCPX)




% [A,ElementIndicies4Target] = ismember(inputElements,targetElements);
% mat2clip(ElementIndicies4Target)
ElementIndicies4Target=[1.00000	2.00000	3.00000	11.00000	5.00000	12.00000	6.00000	7.00000	8.00000	9.00000	10.00000];
noData = find(ElementIndicies4Target==0); 
ElementIndicies4Target(ElementIndicies4Target == 0) = max(ElementIndicies4Target); 
newlyOrderedElements = CPX_OUT(:,ElementIndicies4Target);
%pads rows of NaNs for elements with missing data
newlyOrderedElements(:,noData)=[0]; 
newlyOrderedElements(isnan(newlyOrderedElements))=0; 
CPX_OUT = newlyOrderedElements;  


% % MW = [60.085 79.899	101.961	151.9902	71.846	70.9374	40.311	56.079	61.979	94.203	283.89]; % see matlab guide in excel spreadsheet
% % %     SiO2    TiO2    Al2O3   Cr2O3       FeO     MnO     MgO     CaO    Na2O    K2O    P2O5
% % %      1            2         3          4              5       6           7           8        9
% % 
% % liquidComp_moles = liquidComp./MW;
% % liquidComp_moles = 100*liquidComp_moles/nansum(liquidComp_moles); %%normalizes the molar liquid composition, unneccessary step
% % 
% % XRAT = liquidComp_moles(5)./liquidComp_moles(7);
% % V = FeMgKD*XRAT;
% % XPX = V/(1+V);
% % 
% %     M_eqmin(1) = 48.5;
% %     M_eqmin(2) = 0.75;
% %     M_eqmin(3) = 1.5;
% %     M_eqmin(4) = 0;
% %     M_eqmin(5) = 41.0*XPX;
% %     M_eqmin(6) = 0;
% %     M_eqmin(7) = 41.0*(1-XPX);
% %     M_eqmin(8) = 8.25;
% %     M_eqmin(9) = 0;
% %     M_eqmin(10) = 0;
% %     M_eqmin(11) = 0;
% %     
% % 
% %     eqmin = M_eqmin.*MW;                % this is the bulk comosition of equilibrium augite
% %     eqmin = eqmin./(0.01*sum(eqmin));   % renormalized and in wt%
% %     
% % %       XRAT=A(4)/A(6)
% % %       V=PXK*XRAT
% % %       VO=POK*XRAT
% % %       XOP=VO/(1.+VO)
% % %       XPX=V/(1.+V)
% %       
% %     
% % %        AU(7)=20.60
% % %       AU(4)=28.88*XPX
% % %       AU(6)=28.88*(1.-XPX)
% % %       AU(1)=48.98
% % %       AU(2)=0.97
% % %       AU(3)=0.19
% % %       AU(8)=0.12
% % %SI -AL - TI - FE - P - MG - CA - NA - K - SR - RB - NI
end


